package com.zjh.admin.sys.service.impl;

import com.zjh.admin.sys.model.ResourceEntity;
import com.zjh.admin.sys.model.RoleEntity;
import com.zjh.admin.sys.service.ResourceService;
import com.zjh.admin.sys.service.RoleResourceService;
import com.zjh.admin.sys.service.RoleService;
import com.zjh.core.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author ZWX
 * @Description: 角色资源关联service
 * @date 2018/11/16 20:12
 */
@Service("roleResourceService")
public class RoleResourceServiceImpl implements RoleResourceService {

    @Autowired
    private RoleService roleService;

    @Autowired
    private ResourceService resourceService;

    @Override
    public boolean insertRoleAndResource(ResourceEntity resourceEntity) {
        try {
            //1、添加资源
            resourceService.insert(resourceEntity);
            //2、超级管理员直接赋予该权限
            RoleEntity role = roleService.findByName("超级管理员");
            roleService.addRolePerm(role.getId(), resourceEntity.getId());
            //清空所有用户权限,重新加载权限
            //ShiroAuthenticationManager.clearAllUserAuth();
            return true;

        } catch (Exception e) {
            throw new ServiceException(e);
        }
    }


}
